<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <link rel="stylesheet" href="../css/orderset.css">
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/element-ui/lib/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script src="../js/base.js"></script>

</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统管理
            <small>菜单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input v-model="pagination.queryString" placeholder="菜单名称" style="width: 200px;" class="filter-item"
                          @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPageByCondition()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butP" @click="handleCreatePatent()">新建父菜单</el-button>
            </div>
            <el-table
                    style="width: 100%;margin-bottom: 20px;"
                    current-row-key="id"
                    row-key="path"
                    :data="dataList"
                    stripe
                    highlight-current-row
                    border
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column prop="name" label="菜单名称"></el-table-column>
                <el-table-column label="级别" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.level == '1' ? '父菜单' : '子菜单'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="description" label="菜单说明" align=></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination-container">

            </div>

            <!-- 新增父菜单标签弹层 -->
            <div class="add-form-parent">
                <el-dialog title="新增父菜单" :visible.sync="dialogParentFormVisible">
                    <el-form ref="parentFormData" :model="parentFormData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="父菜单名称">
                                    <el-input v-model="parentFormData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级">
                                    <el-input v-model="parentFormData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <!-- <el-form-item label="图标">
                                     <el-select v-model="parentFormData.icon">-->
                                <el-option label="dashboard" value="fa-dashboard"></el-option>
                                <el-option label="tty" value="fa-tty"></el-option>
                                <el-option label="stethoscope" value="fa-stethoscope"></el-option>
                                </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="说明">
                                    <el-input v-model="parentFormData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogParentFormVisible = false">取消</el-button>
                        <!--<el-button type="success" @click="handleCreateChildren()"></el-button>-->
                        <el-button type="primary" @click="handleAdd()">确定并添加子菜单</el-button>
                    </div>
                </el-dialog>
            </div>

            <!--新增子菜单标签弹层 -->
            <div class="add-form-children">
                <el-dialog title="新增子菜单" :visible.sync="dialogChildrenFormVisible">
                    <el-form ref="childrenFormData" :model="childrenFormData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称">
                                    <el-input v-model="childrenFormData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级">
                                    <el-input v-model="childrenFormData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-form-item label="访问路径">
                                <el-input v-model="childrenFormData.linkUrl"/>
                            </el-form-item>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="说明">
                                    <el-input v-model="childrenFormData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogChildrenFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAddChildren()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 编辑父菜单标签弹层 -->
            <div class="edit-form-parent">
                <el-dialog title="编辑父菜单" :visible.sync="dialogFormVisible4EditParent">
                    <el-form ref="parentFormData" :model="parentFormData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="父菜单名称">
                                    <el-input v-model="parentFormData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级">
                                    <el-input v-model="parentFormData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <!-- <el-form-item label="图标">-->
                                <!--  <el-select v-model="parentFormData.icon">-->
                                <el-option label="dashboard" value="fa-dashboard"></el-option>
                                <el-option label="tty" value="fa-tty"></el-option>
                                <el-option label="stethoscope" value="fa-stethoscope"></el-option>
                                </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="说明">
                                    <el-input v-model="parentFormData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4EditParent = false">取消</el-button>
                        <el-button type="success" @click="handleEditParent()">确定</el-button>
                        <el-button type="primary" @click="handleCreateMoreChildren()">添加子菜单</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑子菜单标签弹层 -->
            <div class="edit-form-children">
                <el-dialog title="编辑子菜单" :visible.sync="dialogFormVisible4EditChildren">
                    <el-form ref="childrenFormData" :model="childrenFormData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称">
                                    <el-input v-model="childrenFormData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级">
                                <el-input v-model="childrenFormData.priority"/>
                            </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-form-item label="访问路径">
                                <el-input v-model="childrenFormData.linkUrl"/>
                            </el-form-item>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="说明">
                                    <el-input v-model="childrenFormData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4EditChildren = false">取消</el-button>
                        <el-button type="primary" @click="handleEditChildren()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>

    var vue = new Vue({
        el: '#app',
        data: {

            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },
            dataList: [],//列表数据
            parentFormData: {//父表单数据
            },
            childrenFormData: {//子表单数据
            },

            dialogParentFormVisible: false,//控制添加父菜单窗口显示/隐藏
            dialogChildrenFormVisible: false,//控制添加子菜单窗口显示/隐藏
            dialogFormVisible4EditParent: false,//控制编辑父菜单窗口显示/隐藏
            dialogFormVisible4EditChildren: false,//控制编辑子菜单窗口显示/隐藏
            rules: {//校验规则
                priority: [{required: false, message: '优先级为选填项', trigger: 'blur'}],
                name: [{required: true, message: '菜单名称为必填项', trigger: 'blur'}]
            },
            backend_url: BACKEND_URL,
        },
        created() {
            this.findPage();
            /*axios.get(this.backendUrl + "/menu/findAll.do").then((res) => {
                this.dataList = res.data.data;
            }).catch((err) => {
                console.log(err);
            });*/
        },
        methods: {
            //编辑父菜单
            handleEditParent() {
                //0. 校验数据
                //1. 获取表单数据
                //2. 发送ajax请求
                axios.defaults.withCredentials = true;
                axios.post(this.backend_url + "/menu/editParent.do", this.parentFormData).then((res) => {
                    //3. 提示结果
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : 'error'
                    });
                    //4. 关闭窗口
                    this.dialogFormVisible4EditParent = false;
                    //5. 重新查询
                    this.findPage();
                }).catch((err) => {
                    this.showMessage(err);
                });
            },
            //编辑子菜单
            handleEditChildren() {
                //0. 校验数据
                //1. 获取表单数据
                //2. 发送ajax请求
                axios.defaults.withCredentials = true;
                axios.post(this.backend_url + "/menu/editChildren.do", this.childrenFormData).then((res) => {
                    //3. 提示结果
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : 'error'
                    });
                    //4. 关闭窗口
                    this.dialogFormVisible4EditChildren = false;
                    //5. 重新查询
                    this.findPage();
                }).catch((err) => {
                    this.showMessage(err);
                });
            },
            //添加父菜单
            handleAdd() {
                //0. 校验数据
                //1. 获取表单数据
                //2. 发送ajax请求
                axios.defaults.withCredentials = true;
                axios.post(this.backend_url + "/menu/addParent.do", this.parentFormData).then((res) => {
                    //3. 提示成功/失败
                    if (res.data.flag) {
                        //1. 重置表单
                        this.resetChildrenForm();
                        //5. 返回parentMenuId
                        this.childrenFormData.parentMenuId = res.data.data.id;
                        //2. 弹出窗口
                        this.dialogChildrenFormVisible = true;
                        //4. 关闭窗口
                        this.dialogParentFormVisible = false;
                    } else {
                        this.$message({
                            type: "error",
                            message: res.data.message
                        })
                    }
                    //5. 重新查询
                    //this.findPage();
                }).catch((err) => {
                    this.showMessage(err);
                });
            },
            //添加子菜单
            handleAddChildren() {
                //0. 校验数据
                //1. 获取表单数据
                //2. 发送ajax请求
                axios.defaults.withCredentials = true;
                axios.post(this.backend_url + "/menu/addChildren.do", this.childrenFormData).then((res) => {
                    //3. 提示结果
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : 'error'
                    });
                    //4. 关闭窗口
                    this.dialogChildrenFormVisible = false;
                    //5. 重新查询
                    this.findPage();
                }).catch((err) => {
                    this.showMessage(err);
                });
            },
            //分页查询
            findPage() {
                //分页参数
                let params = {
                    currentPage:this.pagination.currentPage,//页码
                    pageSize:this.pagination.pageSize,//每页显示的记录数
                    queryString:this.pagination.queryString//查询条件
                };
                //封装分页查询参数
                axios.post(this.backend_url + "/menu/findAll.do",params).then((res) => {
                    this.dataList = res.data.rows;
                    this.pagination.total = res.data.total;
                }).catch((err) => {
                    console.log(err);
                });
            },
            //查询按钮使用的分页查询
            findPageByCondition() {
                //若有查询条件, 把当前页码设置为第一页
                if(this.pagination.queryString != null && this.pagination.queryString.length >0){
                    this.pagination.currentPage = 1;
                }
                //调用分页查询
                this.findPage();
            },
            // 重置表单
            resetForm() {
                //1. 重置父菜单数据
                this.parentFormData = {};
                //2. 重置子菜单数据
                this.childrenFormData = {};
            },
            // 重置父表单
            resetParentForm() {
                //1. 重置父菜单数据
                this.parentFormData = {};
            },
            // 重置子表单
            resetChildrenForm() {
                //2. 重置子菜单数据
                this.childrenFormData = {};
            },
            // 弹出添加父菜单窗口
            handleCreatePatent() {
                //1. 重置表单
                this.resetParentForm();
                //2. 弹出窗口
                this.dialogParentFormVisible = true;

            },
            // 弹出添加子菜单窗口
            handleCreateChildren() {
                //1. 重置表单
                this.resetChildrenForm();
                //2. 弹出窗口
                this.dialogChildrenFormVisible = true;
            },
            // 弹出再添加子菜单窗口
            handleCreateMoreChildren() {
                //handleEditParent
                this.handleEditParent();
                //1. 重置子表单
                this.resetChildrenForm();
                //set parentMenuId
                this.childrenFormData.parentMenuId = this.parentFormData.id;
                //2. 弹出窗口
                this.dialogChildrenFormVisible = true;
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                //0. 清空表单
                this.resetForm();
                //1. 获取id
                var id = row.id;
                // get level
                var level = row.level;
                //console.log(id);
                //console.log(level);
                if (level === 1) {
                    //2. 发送ajax
                    axios.defaults.withCredentials = true;
                    axios.get(this.backend_url + "/menu/findParentMenuById.do?id=" + id).then((res) => {
                        //3. 显示窗口
                        if (res.data.flag) {
                            this.dialogFormVisible4EditParent = true;
                            this.parentFormData = res.data.data;
                        }
                    }).catch((err) => {
                        this.showMessage(err);
                    });
                } else {
                    //2. 发送ajax
                    axios.defaults.withCredentials = true;
                    axios.get(this.backend_url + "/menu/findChildrenMenuById.do?id=" + id).then((res) => {
                        //3. 显示窗口
                        if (res.data.flag) {
                            this.dialogFormVisible4EditChildren = true;
                            this.childrenFormData = res.data.data;
                        }
                    }).catch((err) => {
                        this.showMessage(err);
                    });
                }
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //currentPage为切换后的页码
                this.pagination.currentPage = currentPage;
                //再次查询
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                //console.log(row);
                this.$confirm('此操作将永久删除该菜单, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //1. 获取id
                    var id = row.id;
                    // get level
                    var level = row.level;
                    //console.log(id);
                    //console.log(level);
                    if (level == 1) {
                        //2. 发送ajax
                        axios.defaults.withCredentials = true;
                        axios.get(this.backend_url + "/menu/delParentById.do?id=" + id).then((res) => {
                            //3. 提示结果
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? 'success' : 'error'
                            });
                            if (res.data.flag && this.dataList.length === 1 && this.pagination.currentPage !== 1){ //如果数组长度为1
                                this.pagination.currentPage = this.pagination.currentPage - 1; //当前页=当前页-1
                                if (this.pagination.currentPage === 0){ //如果当前页===0时
                                    this.pagination.currentPage = 1;//默认当前页为1
                                }
                            }
                            //4. 重新查询
                            this.findPage();
                        }).catch((err) => {
                            this.showMessage(err);
                        });
                    } else {
                        //2. 发送ajax
                        axios.defaults.withCredentials = true;
                        axios.get(this.backend_url + "/menu/delChildrenById.do?id=" + id).then((res) => {
                            //3. 提示结果
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? 'success' : 'error'
                            });
                            //4. 重新查询
                            this.findPage();
                        }).catch((err) => {
                            this.showMessage(err);
                        });
                    }
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    })
</script>
</html>
